/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-09-22
*/
package com.rzico.basics.service;

import com.alipay.api.domain.OrderItem;
import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.Member;
import com.rzico.basics.entity.Product;
import com.rzico.basics.entity.ProductPrice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.basics.mapper.ProductPriceMapper;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 商品价格表业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class ProductPriceService extends BaseServiceImpl<ProductPrice, String> {

    @Autowired
    private ProductPriceMapper productPriceMapper;

    @Override
    public BaseMapper<ProductPrice, String> getMapper() {
        return productPriceMapper;
    }

    public BigDecimal getPrice(Long enterpriseId,Long memberId,Long productId) {

        Map<String,Object> params = new HashMap<>();
        params.put("enterpriseId",enterpriseId);
        params.put("memberId",memberId);
        params.put("productId",productId);
        List<ProductPrice> priceList = productPriceMapper.selectList(params);
        if (priceList.size()>0) {
            return priceList.get(0).getPrice();
        } else {
            return null;
        }

    }


    public void addPrice(Long enterpriseId,Long memberId,Long productId,BigDecimal price) {

        if (price.setScale(2,BigDecimal.ROUND_HALF_DOWN).compareTo(BigDecimal.ZERO)==0) {
            return;
        }

        Map<String,Object> params = new HashMap<>();
        params.put("memberId",memberId);
        params.put("productId",productId);
        params.put("enterpriseId",enterpriseId);
        ProductPrice productPrice = null;
        List<ProductPrice> priceList = productPriceMapper.selectList(params);
        if (priceList.size()>0) {
            productPrice = priceList.get(0);
        } else {
            productPrice = new ProductPrice();
        }

        productPrice.setEnterpriseId(enterpriseId);
        productPrice.setCreateDate(new Date());
        productPrice.setMemberId(memberId);
        productPrice.setPrice(price);
        productPrice.setProductId(productId);

        if (productPrice.getId() == null) {
            productPriceMapper.insertUseGeneratedKeys(productPrice);
        } else {
            productPriceMapper.updateByPrimaryKeySelective(productPrice);
        }

    }

}
